package me.sdau.spider.openapi.repository;

import me.sdau.spider.openapi.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.Date;


public interface UserRepository extends JpaRepository<User, Long> {


    /**
     * 通过 account 查找用户信息
     *
     * @param account 用户名
     * @param now     当前时间
     * @return
     */
    @Query("select u from User u where u.account = :account and u.validityDate >= :now")
    User findByAccount(@Param("account") String account, @Param("now") Date now);

    User findByAccount(String account);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("update User set api=:api where id=:id")
    int updateApiById(@Param("api") String api, @Param("id") Long id);
}
